package uni.UNIBEE7B8D.Adapter.study;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import uni.UNIBEE7B8D.Adapter.ClassClickOnListener;
import uni.UNIBEE7B8D.Adapter.RecycClickOnListener;
import uni.UNIBEE7B8D.Enty.CommonClass;
import uni.UNIBEE7B8D.Enty.HomeClass;
import uni.UNIBEE7B8D.R;

public class BanjiRecycAdapter  extends RecyclerView.Adapter<BanjiRecycAdapter.MyViewHolder> {
    private List<HomeClass.ResultBean> fruitList;
    private Context context;
    private ClassClickOnListener recycClickOnListener;

    public ClassClickOnListener getRecycClickOnListener() {
        return recycClickOnListener;
    }

    public void setRecycClickOnListener(ClassClickOnListener recycClickOnListener) {
        this.recycClickOnListener = recycClickOnListener;
    }

    public BanjiRecycAdapter(List<HomeClass.ResultBean> fruitList, Context context) {
        this.fruitList = fruitList;
        this.context = context;
    }

    @NonNull
    @Override
    public BanjiRecycAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_banji_view,parent,false);
        final BanjiRecycAdapter.MyViewHolder myViewHolder = new BanjiRecycAdapter.MyViewHolder(itemView);
        return myViewHolder;
    }

//    @NonNull
//    @Override
//    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        return null;
//    }

    @Override
    public void onBindViewHolder(@NonNull BanjiRecycAdapter.MyViewHolder holder, int position) {
        HomeClass.ResultBean horItem = fruitList.get(position);
        RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(20));//图片圆角为20

        Glide.with(context).load(horItem.getCoverUrl()).apply(options).error(R.mipmap.ic_launcher).into(holder.image);

        holder.title.setText(horItem.getName());
        holder.cost.setText("￥"+horItem.getCost()+"元");
        holder.memberCost.setText("会员价" +horItem.getMemberCost()+"元");
        holder.collectCount.setText(horItem.getCollectCount()+"人收藏");
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeClass.ResultBean fruit = fruitList.get(position);
                recycClickOnListener.onItemClick(fruit);
            }
        });
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        View fruitView;

        TextView title,cost,memberCost,collectCount;
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            fruitView = itemView;
            title = itemView.findViewById(R.id.title);
            image = itemView.findViewById(R.id.image);
            cost = itemView.findViewById(R.id.cost);
            memberCost = itemView.findViewById(R.id.memberCost);
            collectCount = itemView.findViewById(R.id.collectCount);
        }
    }

    @Override
    public int getItemCount() {
        return fruitList.size();
    }
}
